package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author: hy
 * @date:2025/9/17
 * @description: TODO 购买VIP {"1003":VipStrategy}
 * <Bean id="1003" class="com.atguigu.tingshu.user.strategy.impl.VipStrategy"></Bean>
 */
@Service("1003")
public class VipStrategy implements ItemTypeStrategy {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //购买vip user_vip_service - 可能要续期
        //获取购买时长；vip_service_config.id;
        Long vipServiceId = userPaidRecordVo.getItemIdList().get(0);
        // 获取vip服务信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceId);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        //保存数据
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        Date currentTime = new Date();
        userVipService.setStartTime(currentTime);
        //过期时间；第一种：第一次购买 第二种：续期
        //判断当前用户是否是vip并且未过期，续期
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
/*                //属于续期
                LocalDateTime localDateTime = new LocalDateTime(userInfo.getVipExpireTime());
                Date expireTime = localDateTime.plusMonths(serviceMonth).toDate();
                System.out.println("过期时间:" + expireTime);
                userVipService.setExpireTime(expireTime);*/
            currentTime = userInfo.getVipExpireTime();
        }
/*            else {
                //属于第一次购买
                userVipService.setExpireTime(new LocalDateTime(new Date()).plusMonths(serviceMonth).toDate());
            }*/
        //创建对象
        LocalDateTime localDateTime = new LocalDateTime(currentTime);
        //获取过期时间
        Date expireTime = localDateTime.plusMonths(serviceMonth).toDate();
        //设置过期时间
        userVipService.setExpireTime(expireTime);
        //保存数据
        userVipServiceMapper.insert(userVipService);
        //修改用户vip标识
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        userInfoMapper.updateById(userInfo);
    }
}
